/*package com.gohi.main.wxapi.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gohi.main.wx.wxadmin.hbm.WxAdmin;
import com.gohi.main.wxapi.process.*;
import com.gohi.main.wxapi.service.IwxControl;
import com.gohi.main.wxapi.util.AccoutUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.gohi.core.web.action.BaseAction;
import com.gohi.main.wxaccount.hbm.WxAccount;
import com.gohi.main.wxaccount.service.IWxAccountService;
import com.gohi.main.wxmenu.hbm.WxMenu;
import com.gohi.main.wxmenu.service.IWxMenuService;
import com.gohi.main.wxmsgbase.hbm.WxMsgbase;
import com.gohi.main.wxmsgbase.service.IWxMsgbaseService;

import com.gohi.main.wxfensi.hbm.WxFensi;
import com.gohi.main.wxfensi.service.IWxFensiService;

*//**
 * 菜单action
 * 
 * @author Administrator
 * 
 *//*
public class Menuction extends BaseAction {
	Logger logger = Logger.getLogger(Menuction.class);
	

	private String id;// 唯一的区分用户ID
	// 消息触发的关键
	private IWxMsgbaseService wxMsgbaseService;
	private WxMsgbase msg;
	// 微信粉丝
	private IWxFensiService wxFensiService;
	private WxFensi fs;

	// 菜单数据
	private IWxMenuService wxMenuService;
	private WxMenu menu;

	// 粉丝信息
	private IwxControl wxControl;

	public String publishMenu() throws IOException {
		WxAdmin manage = (WxAdmin) this.getSession().getAttribute("admin");
		id = manage.getParent_id();
		if (id == null || id.equals("none")) {
			this.getResponse().getWriter().print(false);
			return null;
		}
		acc = wxAccountService.getById(id);// 获取account
		boolean l = publishMenu(acc.getAppid(), acc.getAppsecret());

		this.getResponse().getWriter().print(l);
		return null;
	}

	// 拉取粉丝列表
	public String fs() throws IOException {
		WxAccount ac = AccoutUtil.getAccount(getRequest(), id);
		// 取地址
		if (ac == null) {
			this.getResponse().getWriter().print(0);
			return null;
		}
		String url = WxApi.getFansList(ac.getAccess_token(), "");

		// System.err.println(ac.getAccess_token()+ac.getLasttime());
		JSONObject jsonObject = WxApi.httpsRequest(url, "GET", null);
		// 暂时没写 同步一千以上的粉丝
		// System.err.println("获取总粉丝:"+jsonObject.getString("count"));
		if (jsonObject.containsKey("errcode")) {
			//System.err.println("错..............");
			AccoutUtil.RefAccount(getRequest(), id);
			this.getResponse().getWriter().print("error");
			return null;
		}
		JSONArray openid = jsonObject.getJSONObject("data").getJSONArray(
				"openid");

		int size = 0;// 同步的粉丝数量
		for (int i = 0; i < openid.size(); i++) {
			// 循环遍历
			String uname = openid.get(i).toString();
			if (wxFensiService.getById(uname) == null) {
				// System.err.println("没有查询到！");
				
				boolean isok =wxControl.saveAccountFans(uname, ac.getAppid(),
						ac.getAppsecret(), ac.getId(),ac.getAccess_token());
			if(isok==true){
				size++;
			}
			}

			// System.err.println();
		}
		this.getResponse().getWriter().print(size);
		// System.err.println(p);
		// System.err.println(p.split(",").length);
		// this.getResponse().getWriter().print(jsonObject.toString());

		return null;
	}

	// 创建菜单
	public boolean publishMenu(String appId, String appSecret) {
		String token = WxApi.getToken(appId, appSecret).getAccessToken();
		String url = WxApi.getMenuCreateUrl(token);
		String menus = getMenus();
		JSONObject jsonObject = WxApi.httpsRequest(url, "POST", menus);
		System.err.println(jsonObject.toString());
		if (null != jsonObject) {
			if (0 != jsonObject.getInt("errcode")) {
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}

	*//**
	 * 此方法是构建菜单对象的；构建菜单时，对于 key 的值可以任意定义； 当用户点击菜单时，会把key传递回来；对已处理就可以了
	 * 
	 * @param menu
	 * @return
	 *//*
	private JSONObject getMenuJSONObj(WxMenu menu) {
		JSONObject obj = new JSONObject();
		obj.put("name", menu.getName());
		obj.put("type", menu.getMtype());
		if ("click".equals(menu.getMtype())) {// 事件菜单
			if ("fix".equals(menu.getEventType())) {// fix 消息
				obj.put("key", "_fix_" + menu.getMsgId());// 以 _fix_ 开头
			} else {
				if (StringUtils.isEmpty(menu.getInputCode())) {// 如果inputcode
																// 为空，默认设置为
																// subscribe，以免创建菜单失败
					obj.put("key", "subscribe");
				} else {
					obj.put("key", menu.getInputCode());
				}
			}
		} else {// 链接菜单-view
			obj.put("url", menu.getUrl());
		}
		return obj;
	}

	public String getMenus() {
		WxMenu w = new WxMenu();
		w.setParid(id);// 只查询指定的公众号的
		List<WxMenu> menus = wxMenuService.select(w);
		if (menus.size() > 0) {
			List<WxMenu> parentAM = new ArrayList<WxMenu>();
			Map<String, List<JSONObject>> subAm = new HashMap<String, List<JSONObject>>();

			for (WxMenu m : menus) {
				if (m.getParentId().equals("0")) {// 一级菜单
					parentAM.add(m);
				} else {// 二级菜单
					if (subAm.get(m.getParentId().toString()) == null) {
						subAm.put(m.getParentId().toString(),
								new ArrayList<JSONObject>());
					}
					List<JSONObject> tmpMenus = subAm.get(m.getParentId()
							.toString());
					tmpMenus.add(getMenuJSONObj(m));
					subAm.put(m.getParentId().toString(), tmpMenus);
				}
			}

			JSONArray arr = new JSONArray();
			for (WxMenu m : parentAM) {
				System.err.println(m.getId());
				if (subAm.get(m.getId()) != null) {// 有子菜单
					arr.add(getParentMenuJSONObj(m, subAm.get(m.getId())));
				} else {// 没有子菜单
					arr.add(getMenuJSONObj(m));
				}
			}
			JSONObject root = new JSONObject();
			root.put("button", arr);
			// System.err.println(root.toString());
			return JSONObject.fromObject(root).toString();
		}
		return "error";
	}

	private JSONObject getParentMenuJSONObj(WxMenu menu,
			List<JSONObject> subMenu) {
		JSONObject obj = new JSONObject();
		obj.put("name", menu.getName());
		obj.put("sub_button", subMenu);
		return obj;
	}

	// ========================================================
	public IWxAccountService getWxAccountService() {
		return wxAccountService;
	}

	public void setWxAccountService(IWxAccountService wxAccountService) {
		this.wxAccountService = wxAccountService;
	}

	public WxAccount getAcc() {
		return acc;
	}

	public void setAcc(WxAccount acc) {
		this.acc = acc;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public IWxMsgbaseService getWxMsgbaseService() {
		return wxMsgbaseService;
	}

	public void setWxMsgbaseService(IWxMsgbaseService wxMsgbaseService) {
		this.wxMsgbaseService = wxMsgbaseService;
	}

	public WxMsgbase getMsg() {
		return msg;
	}

	public void setMsg(WxMsgbase msg) {
		this.msg = msg;
	}

	public IWxFensiService getWxFensiService() {
		return wxFensiService;
	}

	public void setWxFensiService(IWxFensiService wxFensiService) {
		this.wxFensiService = wxFensiService;
	}

	public WxFensi getFs() {
		return fs;
	}

	public void setFs(WxFensi fs) {
		this.fs = fs;
	}

	public IWxMenuService getWxMenuService() {
		return wxMenuService;
	}

	public void setWxMenuService(IWxMenuService wxMenuService) {
		this.wxMenuService = wxMenuService;
	}

	public WxMenu getMenu() {
		return menu;
	}

	public void setMenu(WxMenu menu) {
		this.menu = menu;
	}

	public IwxControl getWxControl() {
		return wxControl;
	}

	public void setWxControl(IwxControl wxControl) {
		this.wxControl = wxControl;
	}

}
*/